﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmThietBi : Form
    {
        private IList<THIETBI_DV> lstThietBi;
        public static FrmThietBi frm = null;

        public FrmThietBi()
        {
            InitializeComponent();
            InitThietBi();
            frm = this;
        }

        private void InitThietBi()
        {
            THIETBI_DV[] outthietbi;
            var client = new ServiceQLKSClient();
            var error = client.doparserThietBi(out outthietbi, OPTCodeQLKS.OPTCode_THIETBI_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_THIETBI_GETALL_SUCCESS)
            {
                lstThietBi = outthietbi.ToList();
                tHIETBIDVBindingSource.DataSource = lstThietBi;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InsertOrUpdate()
        {
            THIETBI_DV thietbi = new THIETBI_DV();
            if (txtMaTB == null || txtTenTB == null || txtGia == null || txtDVT == null)
            {
                MessageBox.Show("Dữ liệu chưa được nhập hết", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                thietbi.MSTB = txtMaTB.Text;
                thietbi.TENTHIETBI = txtTenTB.Text;
                thietbi.DVT = txtDVT.Text;
                thietbi.GIATB = decimal.Parse(txtGia.Text);
                if (chkCoDinh.Checked == true)
                {
                    thietbi.THIETBICODINH = "1";
                }
                else
                {
                    thietbi.THIETBICODINH = "0";
                }
            }
             THIETBI_DV[] outthietbi;
            var client = new ServiceQLKSClient();
            var error = client.doparserThietBi(out outthietbi, OPTCodeQLKS.OPTCode_THIETBI_INSERTUPDATE, thietbi);
            if (error == ErrorCodeQLKS.CODE_THIETBI_INSERTUPDATE_SUCCESS)
            {
                InitThietBi();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
            txtMaTB.Enabled = false;
        }

        private void DeleteThietBi()
        {
            THIETBI_DV[] outthietbi;
            var client = new ServiceQLKSClient();
            var error = client.doparserThietBi(out outthietbi, OPTCodeQLKS.OPTCode_THIETBI_DELETE, new THIETBI_DV {MSTB=txtMaTB.Text});
            if (error == ErrorCodeQLKS.CODE_THIETBI_DELETE_SUCCESS)
            {
                InitThietBi();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            txtMaTB.Enabled = true;
            txtMaTB.Clear();
            txtMaTB.Focus();
            txtDVT.Clear();
            txtGia.Clear();
            txtTenTB.Clear();
        }

        private void gvThietBi_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = gvThietBi.CurrentRow.DataBoundItem as THIETBI_DV;
            if (item != null)
            {
                if (item.THIETBICODINH.Equals("1"))
                {
                    chkCoDinh.Checked = true;
                }
                else
                {
                    chkCoDinh.Checked = false;
                }
            }
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            InsertOrUpdate();
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            txtMaTB.Clear();
            txtMaTB.Focus();
            txtDVT.Clear();
            txtGia.Clear();
            txtTenTB.Clear();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            DeleteThietBi();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<THIETBI_DV> lsttmp;
            lsttmp = lstThietBi.Where(x => x.TENTHIETBI.Contains(txttimkiem.Text)).ToList();
            tHIETBIDVBindingSource.DataSource = lsttmp;
        }

    }
}
